package com.example.shouybl.fragment;


import android.content.Intent;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.ActivityAdapter;
import com.example.shouybl.adapter.AdPageAdapter;
import com.example.shouybl.bean.ActivityAd;
import com.example.shouybl.bean.ActivityData;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.constant.Path;
import com.example.shouybl.ui.ActionDetailActivity;
import com.example.shouybl.utils.AppAsyncTaskHelper;
import com.example.shouybl.utils.ImageLoaderUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class ActionFragment extends Fragment {

    private PullToRefreshListView ptrActivity;
    private ListView lvActivity;
    private List<ActivityData> activityList = new ArrayList<ActivityData>();
    private ActivityAdapter activityAdapter;
    private int page = 1;

    //广告相关
    private ArrayList<ActivityAd> adList = new ArrayList<ActivityAd>();
    private ArrayList<ImageView> imgList = new ArrayList<ImageView>();
    private ViewPager vpAd;
    private AsyncHttpClient client = new AsyncHttpClient();
    private AdPageAdapter pageAdapter;
    private View headView;

    //title布局相关
    private ImageView ivLeft;
    private TextView tvTitle;
    private ImageView ivRight;
    private Button btnRight;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_action, container, false);
        //加载广告布局
        addAd(inflater);
        initView(v);
        initData();
        ptrActivity.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                initData();
            }
        });

        lvActivity.setAdapter(activityAdapter);
        lvActivity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(BaseApp.getInstance(), ActionDetailActivity.class);
                intent.putExtra("id",activityList.get(position).getId());
                startActivity(intent);
            }
        });
        return v;
    }


    private void initData() {
        TypeReference<AppBaseBean<ActivityData>> typeReference = new TypeReference<AppBaseBean<ActivityData>>() {
        };
        AppAsyncTaskHelper.getInstance().post(Path.PATH_ACTIVITY, getParams(), activityList, ptrActivity, activityAdapter, page, typeReference);
    }

    private RequestParams getParams() {
        RequestParams params = new RequestParams();
        params.add("platform", "2");
        params.add("compare", "2");
        return params;
    }

    //广告数据
    private void addAd(LayoutInflater inflater) {
        headView = inflater.inflate(R.layout.item_add_activity_layout,null);

        vpAd = (ViewPager) headView.findViewById(R.id.vp_ad);
        pageAdapter = new AdPageAdapter(imgList);
        vpAd.setAdapter(pageAdapter);
        initLoad();
    }

    private void initLoad() {
        client.post(Path.PATH_ACTIVITY_TOP, getAdParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<AppBaseBean<ActivityAd>> typeReference = new TypeReference<AppBaseBean<ActivityAd>>() {
                };
                AppBaseBean baseBean = JSONObject.parseObject(responseString, typeReference);
                if (baseBean != null && baseBean.getState().equals("success")) {
                    adList.addAll(baseBean.getInfo());
                }
                for (int i = 0; i < adList.size(); i++) {
                    ImageView iv = new ImageView(getActivity());
                    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    ImageLoader.getInstance().displayImage(adList.get(i).getBimg(), iv, ImageLoaderUtil.getDefaultOptions());
                    imgList.add(iv);
                }
                pageAdapter.notifyDataSetChanged();
            }
        });
    }

    private RequestParams getAdParams() {
        RequestParams params = new RequestParams();
        params.add("platform", "2");
        params.add("pos", "1");
        params.add("compare", "57f703337a98b907413992b2a81199f2");
        return params;
    }

    private void initView(View v) {
        //主体变量
        ptrActivity = (PullToRefreshListView) v.findViewById(R.id.ptr_activity);
        ptrActivity.setMode(PullToRefreshBase.Mode.BOTH);
        lvActivity = ptrActivity.getRefreshableView();
        activityAdapter = new ActivityAdapter(activityList, BaseApp.getInstance());
        ptrActivity.getRefreshableView().addHeaderView(headView);
        //title变量
        ivLeft = (ImageView) v.findViewById(R.id.iv_left);
        tvTitle = (TextView) v.findViewById(R.id.tv_title);
        ivRight = (ImageView) v.findViewById(R.id.iv_right);
        btnRight = (Button) v.findViewById(R.id.btn_right);
        ivLeft.setVisibility(View.GONE);
        tvTitle.setText("社区活动");
        ivRight.setVisibility(View.GONE);
        btnRight.setVisibility(View.GONE);

    }


}
